package com.suning.sawp.web.controller;

import java.security.Principal;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.constants.RegAndBindErrCodeConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.intf.storeman.StoreManService;
import com.suning.sawp.intf.yjhx.OldForNewService;
import com.suning.sawp.web.exception.MyGsonView;

/**
 * 以旧换新接口服务
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("/yjhx")
public class OldForNewController extends DjBaseController {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(OldForNewController.class);

    /**
     * 以旧换新服务
     */
    @Autowired
    OldForNewService oldForNewService;

    /**
     * 门店服务
     */
    @Autowired
    StoreManService storeManService;

    /**
     * 查询以旧换新wap地址<br/>
     * 校验手机号是否绑定易购会员,<br/>
     * 校验易购会员是否绑定易付宝,<br/>
     * 校验易购会员是否激活易付宝
     * 
     * @param principal
     * @param mobileNo
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @RequestMapping("/getOldForNewWap.do")
    public MyGsonView getIndexUrl(Principal principal, String mobileNo) {
        LOGGER.debug("getIndexUrl enter,staffId:{},mobileNo:{}", principal.getName(), mobileNo);
        ReturnMsg<String> ret = new ReturnMsg<String>();
        String staffId = principal.getName();
        if (StringUtils.isBlank(mobileNo)) {
            ret.setError(ErrorCodeConstants.ERR_0034);
        } else {
            try {
                // 查询店员信息
                StoreManDto storeMan = storeManService.queryStoreManAttachInfo(staffId);
                ret = oldForNewService.getIndexUrl(mobileNo, storeMan.getStaffId(), storeMan.getStoreCode());
                LOGGER.debug("getIndexUrl, response:{}", ret);
            } catch (Exception e) {
                ret.setRetFlag(ReturnMsg.FAIL);
                ret.setErrorCode(RegAndBindErrCodeConstants.COMMON_ERR_E999);
                ret.setErrorMessage(
                        RegAndBindErrCodeConstants.COMMON_ERR_MAP.get(RegAndBindErrCodeConstants.COMMON_ERR_E999));
                LOGGER.error("getIndexUrl,staffId:" + principal.getName(), e);
            }
        }
        LOGGER.debug("getIndexUrl exit,staffId:{},mobileNo:{}", principal.getName(), mobileNo);
        return setGsonView(ret);
    }

}
